package com.linesum.code.aop;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * aop使用
 * @author chenzf
 * @date 2017年7月28日
 */
@Component
@Aspect
@SuppressWarnings("static-method")
public class AspectJMonitor {
	private static final Logger LOGGER = LoggerFactory.getLogger(AspectJMonitor.class);

	@Pointcut("@annotation (com.linesum.code.aop.RestfulApi)")
	public void webLog() {
	}

	@Before("webLog()")
	public void doBefore(JoinPoint joinPoint) {
		// 接收到请求，记录请求内容
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		// 记录下请求内容
		LOGGER.info("开始aop监控------------------");
		LOGGER.info("URL : " + request.getRequestURL().toString());
		LOGGER.info("HTTP_METHOD : " + request.getMethod());
		LOGGER.info("IP : " + request.getRemoteAddr());
		LOGGER.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "."
				+ joinPoint.getSignature().getName());
	}
}
